package bdgrade;

import java.awt.EventQueue;

import javax.swing.JFrame;
import java.awt.Color;
import javax.swing.JPanel;

import javax.swing.JLabel;
import java.awt.Font;

import javax.swing.JButton;
import javax.swing.JTextField;

import domain.Stu;

import service.Impl.StuServiceImpl;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.ImageIcon;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;

public class Add_Gui {
	//添加界面
	private JFrame frame;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	//注入service
	private StuServiceImpl stuService=new StuServiceImpl();
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Add_Gui window = new Add_Gui();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public Add_Gui() {
		initialize();
	}

	private void initialize() {
		frame = new JFrame();
		frame.setForeground(Color.BLACK);
		frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		frame.setIconImage(Toolkit.getDefaultToolkit().getImage(Add_Gui.class.getResource("/img/1.jpg")));
		frame.getContentPane().setBackground(Color.BLACK);
		frame.getContentPane().setLayout(null);
		frame.setTitle("添加界面");
		frame.setResizable(false);
		frame.setBounds(100, 100, 554, 501);
		frame.setVisible(true);
		
		//学生信息面板
		JPanel panel = new JPanel();
		panel.setBackground(Color.BLACK);
		panel.setBounds(0, 0, 536, 388);
		frame.getContentPane().add(panel);
		panel.setLayout(null);
		//以下是学生信息和输入框
		JLabel label = new JLabel("\u5B66\u53F7");
		label.setFont(new Font("学号", Font.PLAIN, 20));
		label.setForeground(Color.WHITE);
		label.setBackground(Color.WHITE);
		label.setBounds(14, 50, 50, 50);
		panel.add(label);
		textField = new JTextField();
		textField.setForeground(Color.WHITE);
		textField.setBackground(Color.BLACK);
		textField.setFont(new Font("宋体", Font.PLAIN, 15));
		textField.setBounds(62, 65, 77, 25);
		panel.add(textField);
		textField.setColumns(10);
		
		JLabel label_1 = new JLabel("\u59D3\u540D");
		label_1.setForeground(Color.WHITE);
		label_1.setFont(new Font("姓名", Font.PLAIN, 20));
		label_1.setBounds(171, 50, 50, 50);
		panel.add(label_1);
		textField_1 = new JTextField();
		textField_1.setBackground(Color.BLACK);
		textField_1.setForeground(Color.WHITE);
		textField_1.setFont(new Font("宋体", Font.PLAIN, 15));
		textField_1.setBounds(223, 65, 77, 25);
		panel.add(textField_1);
		textField_1.setColumns(10);
		
		JLabel label_2 = new JLabel("\u73ED\u7EA7");
		label_2.setForeground(Color.WHITE);
		label_2.setFont(new Font("课程", Font.PLAIN, 20));
		label_2.setBounds(14, 113, 50, 50);
		panel.add(label_2);
		textField_2 = new JTextField();
		textField_2.setForeground(Color.WHITE);
		textField_2.setBackground(Color.BLACK);
		textField_2.setFont(new Font("宋体", Font.PLAIN, 15));
		textField_2.setBounds(170, 187, 96, 32);
		panel.add(textField_2);
		textField_2.setColumns(10);
		
		JLabel label_3 = new JLabel("\u8BFE\u7A0B");
		label_3.setForeground(Color.WHITE);
		label_3.setFont(new Font("班级", Font.PLAIN, 20));
		label_3.setBounds(14, 176, 50, 50);
		panel.add(label_3);
		textField_3 = new JTextField();
		textField_3.setForeground(Color.WHITE);
		textField_3.setBackground(Color.BLACK);
		textField_3.setFont(new Font("宋体", Font.PLAIN, 15));
		textField_3.setBounds(170, 124, 96, 32);
		panel.add(textField_3);
		textField_3.setColumns(10);
		
		JLabel label_4 = new JLabel("\u6210\u7EE9");
		label_4.setForeground(Color.WHITE);
		label_4.setFont(new Font("成绩", Font.PLAIN, 20));
		label_4.setBounds(14, 239, 50, 50);
		panel.add(label_4);
		textField_4 = new JTextField();
		textField_4.setForeground(Color.WHITE);
		textField_4.setBackground(Color.BLACK);
		textField_4.setFont(new Font("宋体", Font.PLAIN, 15));
		textField_4.setBounds(62, 254, 77, 25);
		panel.add(textField_4);
		textField_4.setColumns(10);		
		
		JLabel label_5 = new JLabel("");
		label_5.setIcon(new ImageIcon(Add_Gui.class.getResource("/img/1.jpg")));
		label_5.setBounds(203, 133, 319, 217);
		panel.add(label_5);
		
		
		
		
		JPanel panel_1 = new JPanel();
		panel_1.setBackground(Color.BLACK);
		panel_1.setBounds(0, 386, 536, 68);
		frame.getContentPane().add(panel_1);
		panel_1.setLayout(null);
		
		
		JButton button = new JButton("\u70B9\u51FB\u65B0\u589E");
		button.setForeground(Color.WHITE);
		button.setBackground(Color.BLACK);
		button.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				//新增按钮 首先获取信息
				String id=textField.getText();
				String name=textField_1.getText();
				String course=textField_2.getText();
				String classes=textField_3.getText();
				String score=textField_4.getText();
				//判断信息是否正确
				int isok=0;
				if(id!=null&&name!=null&&course!=null&&classes!=null&&score!=null&&id.length()!=0&&name.length()!=0&&course.length()!=0&&classes.length()!=0&&score.length()!=0) {
					isok=1;
				}
				if(isok==0) {
					//说明有漏填,进入错误提示界面
					new ADDFALSE();
				}
				if(isok==1) {
					//修改:
					Stu stu=new Stu();
					stu.setID(Integer.parseInt(id));
					stu.setName(name);
					stu.setCourse(course);
					stu.setClasses(classes);
					stu.setScore(score);
					stuService.add(stu);
					new AddOK();//添加成功,进入成功提示界面					
				}			
			}
		});
		
		//课程下拉列表
		JComboBox CourseBox = new JComboBox();
		CourseBox.setFont(new Font("宋体", Font.PLAIN, 15));
		CourseBox.setForeground(Color.WHITE);
		CourseBox.setBackground(Color.BLACK);
		CourseBox.setBounds(62, 190, 77, 26);
		
		List<String>Courses=stuService.findAllCourse();
		for(String c:Courses) {
			CourseBox.addItem(c);
		}
		CourseBox.addItemListener(new ItemListener() {
			//给下拉列表添加监听事件
			@Override
			public void itemStateChanged(ItemEvent e) {
				
				if(e.getStateChange() == ItemEvent.SELECTED){
//					System.out.println("选中" + e.getItem());
					textField_2.setText((String) e.getItem());				
				}			
			}
		});
		panel.add(CourseBox);
				
				
				
		//班级下拉列表
		JComboBox ClassesBox = new JComboBox();
		ClassesBox.setForeground(Color.WHITE);
		ClassesBox.setBackground(Color.BLACK);
		ClassesBox.setFont(new Font("宋体", Font.PLAIN, 15));
		ClassesBox.setBounds(62, 127, 77, 26);
		List<String>Classes=stuService.findAllClasses();
		for(String cla:Classes) {
			ClassesBox.addItem(cla);
		}
		ClassesBox.addItemListener(new ItemListener() {
			//给下拉列表添加监听事件
			@Override
			public void itemStateChanged(ItemEvent e) {
				
				if(e.getStateChange() == ItemEvent.SELECTED){
//					System.out.println("选中" + e.getItem());
					textField_3.setText((String) e.getItem());
				}			
			}
		});
		panel.add(ClassesBox);
				
				
		button.setFont(new Font("点击新增", Font.PLAIN, 16));
		button.setBounds(309, 13, 100, 30);
		panel_1.add(button);
		
		JButton button_1 = new JButton("\u5173\u95ED");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		button_1.setForeground(Color.WHITE);
		button_1.setBackground(Color.BLACK);
		button_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				frame.dispose();
			}
		});
		button_1.setFont(new Font("关闭", Font.PLAIN, 16));
		button_1.setBounds(422, 13, 100, 30);
		panel_1.add(button_1);

	}
}
